package com.crm.jpaandthymeleaf.service.impl;

import com.crm.jpaandthymeleaf.service.RedisService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.ValueOperations;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.io.Serializable;

/**
 * @author 周敬
 * @version 1.0
 * @packageName com.crm.jpaandthymeleaf.service.impl
 * @fileName RedisServiceImpl
 * @createTime 2021/12/20-20:08-星期一
 * @lastModify 2021/12/20-20:08-星期一
 */
@Slf4j
@Service
public class RedisServiceImpl implements RedisService {
    @Resource
    private RedisTemplate redisTemplate;

    @Override
    public boolean set(String key, Object value) {
        boolean result = false;
        try {
            ValueOperations<Serializable, Object> operations = redisTemplate.opsForValue();
            operations.set(key,value);
            result = true;
        } catch (Exception e){
            log.error("set error:key{},value{}",key,value);
        }
        return result;
    }

    @Override
    public Object get(String key) {
        Object result = null;
        try {
            ValueOperations<Serializable,Object> operations = redisTemplate.opsForValue();
            result = operations.get(key);
        } catch (Exception e) {
            log.error("get error:key{}",key);
        }
        return result;
    }
}
